﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace SGLLv2.Template
{
    public partial class FormSCRUDTemplate : Form
    {
        public FormSCRUDTemplate()
        {
            InitializeComponent();
        }

        // Declare Public Events
        public event EventHandler PreventingDataLoss;
        public event EventHandler SaveReady;
        public event EventHandler SaveWaiting;

        // Declare Global Variables for Editing.
        private bool _isEditing;
        private int _numberOfAlterationsToSave;
        private readonly HashSet<string> _alteredObjects = new HashSet<string>();

        public bool IsEditing
        {
            set
            {
                _isEditing = value;

                labelStatus.Text = _isEditing ? "Editando un registro ya existente" : "Agregando un nuevo registro";
                labelStatus.BackColor = _isEditing ? Color.FromArgb(143, 202, 66) : Color.FromArgb(0, 108, 178); // "7H2RS'
                labelStatus.ForeColor = Color.FromArgb(239, 244, 255);
            }
            get { return _isEditing; }
        }
        /// <summary>
        /// Number of alterations in order to enable the save button.
        /// </summary>
        public int NumberOfAlterationsToSave
        {
            set { _numberOfAlterationsToSave = value; }
            get { return _numberOfAlterationsToSave; }
        }
        /// <summary>
        /// Adding the name of the object that modified with a new value.
        /// </summary>
        /// <param name="value">Name of the object modified</param>
        public void Add(string value)
        {
            // Adding the object to the collection.
            _alteredObjects.Add(value);

            // Update Enable Status of Save Button.
            UpdateSaveButton();
        }
        /// <summary>
        /// Removing the name of the object because it has restore its default value.
        /// </summary>
        /// <param name="value">Name of the object modified</param>
        public void Remove(string value)
        {
            // Remove the object from the collection.
            _alteredObjects.Remove(value);

            // Update Enable Status of Save Button.
            UpdateSaveButton();
        }
        /// <summary>
        /// Update the enable status of the Save Button on the form.
        /// </summary>
        private void UpdateSaveButton()
        {
            if (_alteredObjects.Count.Equals(_numberOfAlterationsToSave))
            {
                // Enable Save Button on Form
                OnSaveReady(new EventArgs());
            }
            else
            {
                // Disable Save Button on Form
                OnSaveWaiting(new EventArgs());
            }
        }

        public int ModifiedItems()
        {
            return _alteredObjects.Count;
        }

        protected virtual void OnPreventingDataLoss(EventArgs e)
        {
            var handler = PreventingDataLoss;
            if (handler != null) handler(this, e);
        }

        protected virtual void OnSaveReady(EventArgs e)
        {
            var handler = SaveReady;
            if (handler != null) handler(this, e);
        }

        protected virtual void OnSaveWaiting(EventArgs e)
        {
            var handler = SaveWaiting;
            if (handler != null) handler(this, e);
        }
    }
}